/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.eweboffice;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.eweboffice.Document;
import com.unis.service.eweboffice.DocumentService;

/**
 * @author CSJ (email:Cao.shengjie777@gmail.com)
 * @since 2013-04-09
 */
@Controller
@RequestMapping("/document")
public class DocumentController {

    private DocumentService documentService;

    /****************************************
     * Insert a instace of Document
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertDocument(@RequestBody Document document) throws Exception {
        document.setStatmentId("Document.insert");
        String status = "failure";
        documentService.insert(document);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Document
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Document document) throws Exception {
        document.setStatmentId("Document.update");
        String status = "failure";
        documentService.update(document);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Document
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Document document) throws Exception {
        document.setStatmentId("Document.delete");
        String status = "failure";
        documentService.batchDelete(document);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Document
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Document document = new Document();
        document.setStatmentId("Document.queryAsList");
        BeanUtils.populate(document, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(document, pageMap);
        List<BaseModel> documentList = documentService.queryAsList(document);
        Map<String, Object> renderMap = GridUIMapper.render(document, documentList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Document
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Document document) throws Exception {
        document.setStatmentId("Document.queryAsObject");
        return documentService.queryAsObject(document);
    }

    @RequestMapping("/page")
    public String forword() {
        return "eweboffice/documentList";
    }

    @Resource
    public void setDocumentService(DocumentService documentService) {
        this.documentService = documentService;
    }
}
